// ignore_for_file: constant_identifier_names

part of 'date_util.dart';

enum DateFormatType {
  y,
  ym,
  ymd,
  hms,
  md,
  hm,
  ymdhms,
  ymdhm,
  ymdam,
  quarter,
}

class DateFormat {
  static String format(
    DateTime date,
    DateFormatType type, {
    String dateSeprator = '-',
    String timeSeprator = ':',
    bool isUnit = false,
  }) {
    var year = date.year.toString();
    var month = date.month.toString().padLeft(2, '0');
    var day = date.day.toString().padLeft(2, '0');
    var hour = date.hour.toString().padLeft(2, '0');
    var minute = date.minute.toString().padLeft(2, '0');
    var seconds = date.second.toString().padLeft(2, '0');

    switch (type) {
      case DateFormatType.y:
        return year;
      case DateFormatType.quarter:
        return '$year年第${date.quarter}季度';
      case DateFormatType.ym:
        if (isUnit) {
          return '$year年$month月';
        }
        return '$year$dateSeprator$month';
      case DateFormatType.ymd:
        if (isUnit) {
          return '$year年$month月$day日';
        }
        return '$year$dateSeprator$month$dateSeprator$day';
      case DateFormatType.hms:
        if (isUnit) {
          return '$hour小时$minute分钟$seconds秒';
        }
        return '$hour$timeSeprator$minute$timeSeprator$seconds';
      case DateFormatType.md:
        if (isUnit) {
          return '$month月$day日';
        }
        return '$month$dateSeprator$day';
      case DateFormatType.hm:
        if (isUnit) {
          return '$hour小时$minute分钟';
        }
        return '$hour$timeSeprator$minute$timeSeprator';
      case DateFormatType.ymdhm:
        if (isUnit) {
          return '$year年$month月$day日 $hour小时$minute分钟';
        }
        return '$year$dateSeprator$month$dateSeprator$day $hour$timeSeprator$minute';
      case DateFormatType.ymdam:
        var am = date.hour <= 12 ? '上午' : '下午';
        if (isUnit) {
          return '$year年$month月$day日 $am';
        }
        return '$year$dateSeprator$month$dateSeprator$day $am';
      default:
        if (isUnit) {
          return '$year年$month月$day日 $hour小时$minute分钟$seconds秒';
        }
        return '$year$dateSeprator$month$dateSeprator$day $hour$timeSeprator$minute$timeSeprator$seconds';
    }
  }

  static DateTime? formatString(
    String? data, {
    String sp1 = '-',
    String sp2 = ':',
  }) {
    if (isNilStr(data)) return null;
    if (data!.contains(sp1)) {
      var list = data.split(' ');
      if (list.isEmpty) return null;
      var dl = list.first.split(sp1);
      if (dl.length != 3) return null;
      var y = numVal(dl[0]).toInt();
      var m = numVal(dl[1]).toInt();
      var d = numVal(dl[2]).toInt();
      int h = 0;
      int mm = 0;
      var s = 0;
      if (list.length == 2) {
        var tl = list.last.split(sp2);
        if (tl.isNotEmpty) {
          h = numVal(tl[0]).toInt();
          if (tl.length > 1) {
            mm = numVal(tl[1]).toInt();
          }
          if (tl.length > 2) {
            s = numVal(tl[2]).toInt();
          }
        }
      }
      return DateTime(y, m, d, h, mm, s);
    }
    return null;
  }

  static String? transformString(
    String? data, {
    String sp1 = '-',
    String sp2 = ':',
    DateFormatType type = DateFormatType.ymdhms,
  }) {
    if (isNilStr(data)) return null;
    if (data!.contains(sp1)) {
      var list = data.split(' ');
      if (list.isEmpty) return null;
      var dl = list.first.split(sp1);
      if (dl.length != 3) return null;
      var y = numVal(dl[0]).toInt();
      var m = numVal(dl[1]).toInt();
      var d = numVal(dl[2]).toInt();
      int h = 0;
      int mm = 0;
      var s = 0;
      if (list.length == 2) {
        var tl = list.last.split(sp2);
        if (tl.isNotEmpty) {
          h = numVal(tl[0]).toInt();
          if (tl.length > 1) {
            mm = numVal(tl[1]).toInt();
          }
          if (tl.length > 2) {
            s = numVal(tl[2]).toInt();
          }
        }
      }
      return format(DateTime(y, m, d, h, mm, s), type);
    }
    return null;
  }
}
